import { Body, Controller, Post, Inject } from '@midwayjs/core';
import { ApiResponse, ApiTags } from '@midwayjs/swagger';
import { Context } from '@midwayjs/koa';
import { ServerResponse } from '../util/response';
import { FlagResDto, Pagination } from '../model';
import {
  VisitorAddDto,
  VisitorIdRequired,
  VisitorItemDto,
  VisitorItemResDto,
} from '../model/visitor.dto';
import { VisitorService } from '../service/visitor.service';

@ApiTags(['访客'])
@Controller('/visitor')
export class VisitorController {
  @Inject()
  ctx: Context;

  @Inject()
  visitorService: VisitorService;

  @Post('/list', { summary: '访客列表' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: VisitorItemResDto,
  })
  async list(@Body() req: Pagination) {
    const params: Pagination = {
      pageNo: 1,
      pageSize: 10,
      ...req,
    };
    const res = await this.visitorService.getList(params);
    return ServerResponse.JSON_TPL({
      list: res.rows,
      pagination: {
        ...params,
        total: res.count,
      },
    });
  }

  @Post('/listByPerson', { summary: '邀请记录' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: VisitorItemResDto,
  })
  async listByPerson(@Body() req: Pagination) {
    const params: Pagination = {
      pageNo: 1,
      pageSize: 10,
      ...req,
    };
    const res = await this.visitorService.listByPerson(
      params,
      this.ctx.state.user.id
    );
    return ServerResponse.JSON_TPL({
      list: res.rows,
      pagination: {
        ...params,
        total: res.count,
      },
    });
  }

  @Post('/add', { summary: '新增访客' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async add(@Body() req: VisitorAddDto) {
    const res = await this.visitorService.addItem(req, '1', this.ctx.state.user.id);
    return ServerResponse.JSON_TPL({
      flag: Object.keys(res).length > 0,
    });
  }

  @Post('/byId', { summary: '访客详情' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: VisitorItemDto,
  })
  async byId(@Body() req: VisitorIdRequired) {
    const res = await this.visitorService.byIdItem(req);
    return ServerResponse.JSON_TPL({
      data: res,
    });
  }
}
